package com.by.service.common.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by.common.bean.PageBean;
import com.by.condition.common.ProductCondition;
import com.by.entity.common.Product;
import com.by.mapper.common.ProductMapper;
import com.by.service.common.ProductService;
import com.by.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Override
    public PageBean queryProduct(ProductCondition productCondition) {
        Page<Product> page=new Page<>(productCondition.getPage(),productCondition.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(StringUtils.isNotEmpty(productCondition.getCategoryCode())){
            queryWrapper.like("code",productCondition.getCategoryCode());
        }
        if(StringUtils.isNotEmpty(productCondition.getCode())){
            queryWrapper.like("code",productCondition.getCode());
        }
        if(StringUtils.isNotEmpty(productCondition.getName())){
            queryWrapper.like("name",productCondition.getName());
        }
        if(StringUtils.isNotEmpty(productCondition.getSpecification())){
            queryWrapper.like("specification",productCondition.getSpecification());
        }
        page= baseMapper.selectPage(page,queryWrapper);
        return new PageBean(page.getCurrent(),page.getSize(),page.getTotal(),page.getRecords());
    }


    @Override
    public PageBean queryProductBySql(ProductCondition productCondition) {
        PageHelper.startPage(productCondition.getPage(),productCondition.getPageSize());
        List<Product> list=baseMapper.queryProductBySql(productCondition);

        PageInfo pageInfo=new PageInfo(list);

        return new PageBean(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),pageInfo.getList());
    }
}
